package fgoScript.entity.guda;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import commons.util.PropertiesUtil;
import destinychild.entity.PointAndColor;
import fgoScript.constant.PointInfo;
import fgoScript.service.CommonMethods;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import fgoScript.constant.GameConstant;
import fgoScript.entity.CardComparator;
import fgoScript.entity.CommonCard;
import fgoScript.entity.PointColor;
import fgoScript.service.EventFactors;
import commons.util.GameUtil;

public class EventGudazi extends AbstractApGudazi {
	private static final Logger LOGGER = LogManager.getLogger(EventGudazi.class);
	@Override
	public void intoAndSelect(int apNum, int acountNum) throws Exception {
		insertIntoEventRoom();
	}
	@Override
	public void fightAndStop(boolean rebootFlag, int apNum, int account) throws Exception {
		fightAndBackForEvent(rebootFlag);
	}
	@Override
	public Point getSuppotServant() {
		return PointInfo.getInstance().getpServantMix();
	}

	@Override
	public void fightOverMethod() {
	}

	private void insertIntoEventRoom() throws Exception {
        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestDown(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestTop(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        // 进入活动本
        Point p4 = PointInfo.getInstance().getpWeekEntrance01();
        GameUtil.getMymouse().mouseMoveByPoint(p4,true,true);

        GameUtil.getMymouse().mousePressAndReleaseForConfirm(null);
        
        GameUtil.delay(GameConstant.DELAY*5);
        
        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestTop(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestDown(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        
        
        
        // 拉滚动条至目标本
        Point p7 = PointInfo.getInstance().getpEventScrollLocate01();
        GameUtil.getMymouse().mouseMoveByPoint(p7,true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        // 点击ap本
        Point p8 = PointInfo.getInstance().getpEventBattleLocate01();
        GameUtil.getMymouse().mouseMoveByPoint(p8,true,true);

        GameUtil.getMymouse().mousePressAndReleaseForConfirm(null);
    }
	
	private void fightAndBackForEvent(boolean rebootFlag) throws Exception {
        // 1回合数字高亮点
        List<PointAndColor> roundPcList = PointInfo.getInstance().getRound01();
        int size = roundPcList.size();
        List<Point> pointList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            pointList.add(roundPcList.get(i).getPoint());
        }
        int eveValue;
        int battleRounds = 0;
        int MaxRounds = 20;
        int rounds = EventFactors.battleRounds;
        // 第一回合
        LOGGER.info("准备战斗，等待。。。。");
        CommonMethods.waitToAttack(null, LOGGER);
        LOGGER.info("第一回合开始：");
        boolean[] hases = {false,false};
        eveValue = GameUtil.getColorEveValue(pointList);
        if (rebootFlag) {
            DEFAULT_SKILL_COLORS = EventFactors.getDefaultSkillColorsFromFile();
        }else {
            DEFAULT_SKILL_COLORS = EventFactors.getSkillColors();
            EventFactors.writeDefaultSkillColors(DEFAULT_SKILL_COLORS);
        }
        String startegy;
        while (eveValue > GameConstant.THRESHOLD && battleRounds < MaxRounds) {
            battleRounds++;
			/*
			  连续使用技能组
			 */
            giveServantSkills(EventFactors.getPreSkills(DEFAULT_SKILL_COLORS));
            CommonMethods.waitToAttack(null, LOGGER);
            startegy = PropertiesUtil.getValueFromSkillsFile("SKILL_STRATEGY");
            switch("".equals(startegy)? GameConstant.NO_SKILL : startegy){
                case GameConstant.NO_SKILL : {
                    CommonMethods.attackNPAAA(LOGGER);
                    break;
                }
                case GameConstant.GO_SECOD_SKILL_FOR_FIRST : {
                    attackBAAForEvent(false, hases);
                    break;
                }
                case GameConstant.GO_ALL_SKILL_FOR_FIRST : {
                    attackBAAForEvent(true, hases);
                    break;
                }
                default : {
                    break;
                }
            }
            // 等待
            CommonMethods.waitToAttack("1", LOGGER);
            eveValue = GameUtil.getColorEveValue(pointList);
        }
        // 第二回合
        // 2回合数字高亮点
        roundPcList = PointInfo.getInstance().getRound02();
        size = roundPcList.size();
        pointList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            pointList.add(roundPcList.get(i).getPoint());
        }
        CommonMethods.waitToAttack(null, LOGGER);
        LOGGER.info("第二回合开始：");
        eveValue = GameUtil.getColorEveValue(pointList);

        boolean hasSelect = false;
        int count = 0;
        PointColor pc;
        boolean ifSecondNP = EventFactors.ifSecondNP;
        while (eveValue > GameConstant.THRESHOLD&& battleRounds < MaxRounds) {
            battleRounds++;
			/*
			  连续使用技能组
			 */
            giveServantSkills(EventFactors.getPreSkills(DEFAULT_SKILL_COLORS));
            if (!hasSelect) {
                // 选择第2个怪物
                Point p_moster01 =PointInfo.getInstance().getpMoster01();
                GameUtil.getMymouse().mouseMoveByPoint(p_moster01,true,true);

                GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
                hasSelect = true;
            }
            if (count==0) {
                //加回避
                giveClothSkills(EventFactors.getRound2ClothSkill());
            }
            pc = CommonMethods.waitToAttack(null, LOGGER);
            if ("attack".equals(pc.getName())) {
                if (rounds==3 ) {
                    if (ifSecondNP) {
                        attackBAAForEvent(true, hases);
                    }else {
                        attackBAAForEvent(false, hases);
                    }
                }else {
                    attackBAAForEvent(true, hases);
                }
            }else {
                LOGGER.info("羁绊了，跳出来");
                break;
            }
            // 等待
            CommonMethods.waitToAttack("3", LOGGER);
            eveValue = GameUtil.getColorEveValue(pointList);
            count++;
        }
        if (rounds==3) {
            // 3回合数字高亮点
            roundPcList = PointInfo.getInstance().getRound03();
            size = roundPcList.size();
            pointList = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                pointList.add(roundPcList.get(i).getPoint());
            }
            // 第三回合
            // 等待
            CommonMethods.waitToAttack(null, LOGGER);
            LOGGER.info("第三回合开始：");
            eveValue = GameUtil.getColorEveValue(pointList);
            hasSelect= false;
            while (eveValue > GameConstant.THRESHOLD && battleRounds < MaxRounds) {
                battleRounds++;
                CommonMethods.waitToAttack("3", LOGGER);
                if (checkFinish()) {
                    break;
                }
				/*
				  连续使用技能组
				 */
                giveServantSkills(EventFactors.getPreSkills(DEFAULT_SKILL_COLORS));
                if (!hasSelect) {
                    // 选择第个怪物
                    Point monstor =EventFactors.getMonsterPoint();
                    if (monstor!=null) {
                        GameUtil.getMymouse().mouseMoveByPoint(monstor,true,true);

                        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
                        CommonMethods.waitToAttack(null, LOGGER);
                        hasSelect = true;
                    }
                }
                // 宝具平A
                attackBAAForEvent(true, hases);
                CommonMethods.waitToAttack("3", LOGGER);
                eveValue = GameUtil.getColorEveValue(pointList);
            }
            LOGGER.info("3链单循环战斗结束");
        }
	}
	/**
	 * 宝具平a攻击
	 * 
	 * @throws Exception 异常
	 */
	private void attackBAAForEvent(boolean goMainNP, boolean[] hases) throws Exception {
		Point pNp01 = PointInfo.getInstance().getNpCheck();
		Point pNp02 = new Point((int)pNp01.getX()+ GameConstant.HEAD_SPACE, (int)pNp01.getY());
		Point pNp03 = new Point((int)pNp01.getX()+ GameConstant.HEAD_SPACE*2, (int)pNp01.getY());
		Color cNp01 = GameUtil.getScreenPixel(pNp01);
		Color cNp02 = GameUtil.getScreenPixel(pNp02);
		Color cNp03 = GameUtil.getScreenPixel(pNp03);
		GameUtil.delay(100);
		Color cNp01_Delay = GameUtil.getScreenPixel(pNp01);
		Color cNp02_Delay = GameUtil.getScreenPixel(pNp02);
		Color cNp03_Delay = GameUtil.getScreenPixel(pNp03);
		int npCount = 0;
		boolean hasNp01 = cNp01.getRGB() != cNp01_Delay.getRGB();
		boolean hasNp02 = cNp02.getRGB() != cNp02_Delay.getRGB();
		boolean hasNp03 = cNp03.getRGB() != cNp03_Delay.getRGB();
		boolean has = hasNp01
				|| hasNp02
				|| hasNp03;
		LOGGER.info("有宝具吗？  " + (has));
		if ( has ) {
			// 等待
			if (hasNp01) {
				if (!hases[0] && goMainNP) {
					// 战斗服技能
					battleSkillForP01();
					hases[0] = true;
					// 等待
					CommonMethods.waitToAttack(null, LOGGER);
					GameUtil.delay(1000);
					/*
					  宝具技能组01
					 */
					giveServantSkills(EventFactors.getNPSkills01(DEFAULT_SKILL_COLORS));
				}
			}
			if (hasNp02) {
				/*
				  宝具技能组02
				 */
				giveServantSkills(EventFactors.getNPSkills02(DEFAULT_SKILL_COLORS));
			}	
			// 等待
			CommonMethods.waitToAttack(null, LOGGER);
			// 蓝色圆板选择
			CommonMethods.blueAttackSelect();
            // 开始点击卡片
			Map<String, List<CommonCard>> scMap;
			if (hasNp01 && goMainNP) {
				scMap = CommonMethods.getWeakCommondCards(CardComparator.getRgbComparotor(), LOGGER);
			}else {
				scMap = CommonMethods.getWeakCommondCards(CardComparator.getBgrComparotor(), LOGGER);
			}
			Point np_np01 = PointInfo.getInstance().getNpNp();
			Point np_np02 = new Point((int) np_np01.getX()+GameConstant.NP_SPACE,(int) np_np01.getY());
			Point np_np03 = new Point((int) np_np01.getX()+GameConstant.NP_SPACE*2,(int) np_np01.getY());
			if (hasNp03) {
				// 宝具
                GameUtil.getMymouse().mouseMoveByPoint(np_np03,true,true);

                GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
				npCount++;
			}
			if (hasNp02) {
				// 宝具
                GameUtil.getMymouse().mouseMoveByPoint(np_np02,true,true);

                GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
				npCount++;
			}
			if (hasNp01 && goMainNP) {
				// 宝具
                GameUtil.getMymouse().mouseMoveByPoint(np_np01,true,true);

                GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
				npCount++;
			}
			List<CommonCard> trueList = scMap.get("trueList");
			List<CommonCard> falseList = scMap.get("falseList");
			int size = trueList.size();
			CommonCard commonCard;
			for (int i = 0; i < size; i++) {
				commonCard = trueList.get(i);
                GameUtil.getMymouse().mouseMoveByPoint(commonCard.getpLoc(),true,true);

                GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
			}
			if (size < (3-npCount)) {
				int num = 2-trueList.size()+1;
				for (int j = 0; j < num; j++) {
                    GameUtil.getMymouse().mouseMoveByPoint(falseList.get(j).getpLoc(),true,true);

                    GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
				}
			}
		} else {
			CommonMethods.attackNPAAA(LOGGER);
		}
		CommonMethods.checkExitCardSelect();
	}
	private void battleSkillForP01() throws Exception {
		// 等待
		CommonMethods.waitToAttack(null, LOGGER);
		// 战斗服选择
		Point p5 = new Point(1229, 359);// 颜色：24;60;107 Color c = new Color(24, 60, 107);
        GameUtil.getMymouse().mouseMoveByPoint(p5,true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
		// 选择技能2
		Point p8 = new Point(1029, 357);// 颜色：252;249;107 Color c = new Color(252, 249, 107);
        GameUtil.getMymouse().mouseMoveByPoint(p8,true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
		// 选择人物
        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getSkillPerson01(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);

		// 等待
		CommonMethods.waitToAttack(null, LOGGER);
		// 战斗服选择
        GameUtil.getMymouse().mouseMoveByPoint(p5,true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
		// 选择技能3
		Point p9 = new Point(1115, 360);// 颜色：69;71;111 Color c = new Color(69, 71, 111);
        GameUtil.getMymouse().mouseMoveByPoint(p9,true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
		// 选择人物
        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getSkillPerson01(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
	}

	public static void main(String[] args) {
		// 1回合数字高亮点
		Point ps1 = new Point(903, 79);// 颜色：200;200;200 Color c = new Color(200, 200, 200);
		Point ps2 = new Point(903, 69);// 颜色：255;255;255 Color c = new Color(255, 255, 255);
		Point ps3 = new Point(904, 74);// 颜色：255;255;255 Color c = new Color(255, 255, 255);
		List<Point> pointList = new ArrayList<>();
		pointList.add(ps1);
		pointList.add(ps2);
		pointList.add(ps3);
		System.out.println(GameUtil.getColorEveValue(pointList));
	}
}
